package com.example.jdbc;

import java.math.BigDecimal;
import java.sql.*;

/**
 * @author lei yu
 * @date 2024/05/15
 */
public class JDBCDemo2 {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1,注册驱动：⼿动加载字节码⽂件到 JVM 中
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 2,连接数据库：参数1：url、参数2：用户、参数3：密码
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "admin", "admin@me4");

        // 3,获取执行SQL语句的对象
        Statement stmt = conn.createStatement();

        // 4,执行SQL
        String sql = "SELECT * FROM bonus;";
        // 执行后的结果集
        ResultSet resultSet = stmt.executeQuery(sql);
        // 5,处理结果
        while (resultSet.next()) {
            // 一列一列获取
//            String ename = resultSet.getString("ename");
            // 从1开始
            int ename = resultSet.getInt(1);
            System.out.println("ename = " + ename);

            BigDecimal sal = resultSet.getBigDecimal("sal");
            System.out.println("sal = " + sal);

        }
        // 6，释放资源
        stmt.close();
        conn.close();

    }
}
